package com.XSQS.storage.index;

import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import org.apache.log4j.Logger;

import com.XSQS.storage.helper.FileName;
import com.XSQS.storage.manager.FileManager;
import com.XSQS.storage.util.XMLSubTree;
import com.XSQS.util.Utility;

public class Inverter {
	private FileManager fm=FileManager.getInstance();
	private Logger logger=Utility.getLogger(null);
	
	private static final int BUF_SIZE=1024; 
	private List<InvertedWordItem> indexBuf4Merge=new LinkedList<InvertedWordItem>();
	
	private List<String> keys4Query=new ArrayList<String>(BUF_SIZE);
	private List<InvertedWordItem> indexBuf4Query=new ArrayList<InvertedWordItem>(BUF_SIZE);
	
	public void merge2Index(XMLSubTree xmlSubTree){
		
	}
	
	public IndexEntry[] getIndexEntries(String key){
		IndexEntry[] result=getIndexEntriesFromBuffer(key);
		if(result==null){
			result=getIndexEntriesFromHardDisk(key);
		}
		return result;
	}
	
	private void flush(){
		
	}
	
	private IndexEntry[] getIndexEntriesFromBuffer(String key){
//		int index=Collections.binarySearch(keys4Query, key);
//		if(index==-1){
//			return null;
//		}else{
//			return (IndexEntry[]) indexBuf4Query.get(index).getEntries().toArray();
//		}
		return null;
	}
	
	private IndexEntry[] getIndexEntriesFromHardDisk(String key){
//		RandomAccessFile metaIndexFile=fm.openFile(FileName.getMetaIndexFile());
//		RandomAccessFile indexFile=fm.openFile(FileName.getIndexFile());
//		if(metaIndexFile==null||indexFile==null){
//			logger.error("Index has not been created.");
//			return null;
//		}
		
		return null;
	}
}
